home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / filbuf.c < prev    next >
C/C++ Source or Header  |  1991-04-13  |  1KB  |  48 lines

  1. /*
  2.  * fill and process an input buffer
  3.  *    called only when fp->_cnt < 0
  4.  *
  5.  * more hacks, the initial impl bit!
  6.  *
  7.  *    ++jrb    bammi@dsrgsun.ces.cwru.edu
  8.  *
  9.  * do not remember EOF if input comes from a tty (er)
  10.  *
  11.  */
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <memory.h>
  18. #include "lib.h"
  19.  
  20. #ifdef __GNUC__
  21. #define alloca __builtin_alloca
  22. #endif
  23.  
  24. int _filbuf(fp)
  25. FILE *fp;
  26. {
  27.     register unsigned int f;
  28.     register long got;
  29.     
  30.     f = fp->_flag;
  31.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  32.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  33.     return(EOF);
  34.  
  35.     /* if this is stdin &  a tty, and stdout is line buffered, flush it */
  36.     if((fp == stdin) && (f & _IODEV) && (stdout->_flag & _IOLBF))
  37.     (void)fflush(stdout);
  38.  
  39.     fp->_ptr = fp->_base;
  40.     if((got = _read(fp->_file, fp->_base, (long)fp->_bsiz)) <= 0)
  41.     {   /* EOF or error */
  42.     fp->_flag |= ((got == 0) ? ((f & _IODEV) ? 0 : _IOEOF) : _IOERR);
  43.     return EOF;
  44.     }
  45.     fp->_cnt = got - 1;
  46.     return *(fp->_ptr)++;
  47. }
  48.